package com.yes.yll;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebFilter(urlPatterns = "/yll/*")
public class CoreFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        //初始化函数

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //执行拦截的逻辑代码

        //强制类型转换
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response =(HttpServletResponse)servletResponse;

        String uri = request.getRequestURI();
        if("/yll/login.jsp".equals(uri) || uri.endsWith("css")){
            filterChain.doFilter(request,response);
            return;
        }


        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if(uid ==null){
            //未登录

            response.sendRedirect("/yll/login.jsp");
            return;

        }
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
        //当过滤器被销毁时被执行

    }
}
